gRPC Basics tutorial実況中継
チュートリアルでわかること
Pcotobufのcomplireでserver/clientのコード生成
Goで単純なserver/clientのサービスを作ったときに、gRPC APIををつかったAPIを作成できる
前提知識
つくるアプリ
simple route mapping application
クライアントがルートの特徴情報を取得して、ルートの要約を作ってtrafific updateなどのルート情報をサーバーや他のクライアントと交換する
kadoyau.icon 地図が関係するアプリらしい
all the complexity of communication between different languages and environments is handled for you by gRPC.
サービス定義をする
gRPC serviceを定義する
.protoにfunctionを定義する
code:route_guide.proto
// Interface exported by the server.
service RouteGuide {
// A simple RPC.
//
// Obtains the feature at a given position.
//
// A feature with an empty name is returned if there's no feature at the given
// position.
rpc GetFeature(Point) returns (Feature) {}
// A server-to-client streaming RPC.
//
// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}
// A client-to-server streaming RPC.
//
// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}
// A Bidirectional streaming RPC.
//
// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}
request/responseのmessageTypeにPoint型などを使っているが、この定義もroute_guide.protoに書く
code:route_guide.proto
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}